/*
 * ---------------------------------------------------------------------
 * Copyright (c) 2014, 2015 All Right Reserved
 * This software is developed for study and improve coding skill ...
 *
 * Project:  Excellent Coding< EC >
 *
 * ecThread.h
 * This file for EC thread define and encapsulation.
 *
 * Eamil:   epengao@126.com
 * Author:  Peter Gao
 * Version: Intial first version.
 * --------------------------------------------------------------------
 */

#ifndef EC_THREAD_H
#define EC_THREAD_H

#include "ecthreadI.h"
#include "ecMutex.h"
#include "ecCQueue.h"
#include "ecMsgProcI.h"
#include "ecSemaphore.h"

#ifdef _WIN32
#include <windows.h>
#else
#include <pthread.h>
#endif

namespace ec {

#define MAX_THREAD_MSG_QUEUE 512

class ecThread : public ecThreadI
{
public:
    ecThread(
        EC_PCHAR pThreadName = EC_NULL,
        ecThreadType nThreadType = ecThreadType_Nomal,
        EC_U32 nMsgListSize = MAX_THREAD_MSG_QUEUE);
    virtual ~ecThread();

    virtual void Run();
    virtual void Pause();
    virtual void Stop();
    virtual EC_U32 PostMsg(ecMessage* pMsg = NULL);

protected:
    void virtual OnMsg();
    static void* ThreadFuncEntry(void* pArg);

protected:
    EC_PCHAR       m_pName;
    ecThreadType   m_nType;
    ecThreadStatus m_nStatus;
    ecMsgProcI*    m_pMsgList;
    ecMutex        m_mtxMsgList;
    ecSemaphore    m_semMsgSignal;
#ifdef _WIN32
    DWORD          m_nThreadID;
    HANDLE         m_hThread;
#else
    pthread_t      m_ThreadHandle;
#endif
};

} /* namespace ec */
#endif /* EC_THREAD_H */